package com.dingwen.treasure.websocket.server.config;

import com.dingwen.treasure.websocket.server.handler.TreasureWebsocketHandler;
import com.dingwen.treasure.websocket.server.interceptor.WebSocketHandshakeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * 　WebSocketConfig: 配置WebSocket的访问通道
 * 　@author dingwen
 * 　@date 2022/8/31
 *
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource(name = "treasureWebsocketHandler")
    private TreasureWebsocketHandler webSocketHandler;

    @Resource(name = "webSocketHandshakeInterceptor")
    private WebSocketHandshakeInterceptor webSocketHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        // 注册访问通道
        webSocketHandlerRegistry.addHandler(webSocketHandler,"/")
                .addInterceptors(webSocketHandshakeInterceptor)
                .setAllowedOrigins("*");
    }
}
